#ifndef	WF_EXTEXCITATION_GLOBALS
#define	WF_EXTEXCITATION_GLOBALS
#include <includes.h>
#include "WF_ExtExcitation.h"

static uint8_t ShutDownAlarm_WorkStepHandle(uint8_t *TempWFStep,uint8_t *TempWFPeriod,uint8_t *WFStaChange,uint8_t Solution)
{
  if(GetFlagShareBufferDataContent(NULL,CfgOperaType_None,FlagShareBuffer_GenWorkingSta,GenWorkStaOrd_AlarmShutDown))//检查控制器的运行状态是否在停机告警态
  {
  	if(Solution&GenFaultWay_Action)
    {
        SetNewWorkFlowStepStatus(TempWFStep,TempWFPeriod,WFStaChange,ExtExcitationStep_endStep);
        return(DEF_TRUE);
    }
  }
  
  return(DEF_FALSE);
}

uint8_t ExtExcitationStep_1st(uint8_t TempWorkflow,uint8_t TempWFStep,uint8_t TempWFPeriod, uint8_t *TempOutAlarm)
{
    uint8_t ActInform = DEF_FALSE,WFStaChange = DEF_FALSE;

    CurrProcCountdownEnable = DEF_TRUE;
    SetNextNewWorkFlowStepStatus(&TempWFStep,&TempWFPeriod,&WFStaChange,ExtExcitationStep_Maximum);
    /* Update Workflow--Step and Mode */
    UpdateWorkflowModeStatus(&TempWorkflow, &TempWFStep,&TempWFPeriod, &WFStaChange);
    return (ActInform);
}
uint8_t ExtExcitationStep_2st(uint8_t TempWorkflow,uint8_t TempWFStep,uint8_t TempWFPeriod, uint8_t *TempOutAlarm)
{
    uint8_t ActInform = DEF_FALSE,WFStaChange = DEF_FALSE;
    ActInform |= SwitchBefCheckStep(TempWorkflow,TempWFStep,TempWFPeriod,ExtExcitationStep_Maximum,
        share_parameterCfgData_st.ceilingExciCfg_st.EngineInitStart_time,share_parameterCfgData_st.ceilingExciCfg_st.EngineInitSpeed,ShutDownAlarm_WorkStepHandle);
    
    /* Update Workflow--Step and Mode */
    UpdateWorkflowModeStatus(&TempWorkflow, &TempWFStep,&TempWFPeriod, &WFStaChange);
    return (ActInform);
}
/**
 * @brief 调节励磁分压器电机到预设的极限励磁电压值
 * 
 * @param TempWorkflow 
 * @param TempWFStep 
 * @param TempWFPeriod 
 * @param TempOutAlarm 
 * @return uint8_t 
 */
uint8_t ExtExcitationStep_3st(uint8_t TempWorkflow,uint8_t TempWFStep,uint8_t TempWFPeriod, uint8_t *TempOutAlarm)
{
    uint8_t ActInform = DEF_FALSE,WFStaChange = DEF_FALSE;
    if(TempWFPeriod==WorkflowPeriod_Entry)
    {
        // 闭合励磁分压SW
        ModifyFlagShareBufferDataContent(NULL,CfgOperaType_None,ShareBuffer_Outputs,RelayOutput_EXCV_SW,BitOperaOpt_Set);
        ActInform |= WorkflowOutRequestBit_RelayOut;
        SetNewWorkFlowPeriodStatus(&TempWFPeriod,&WFStaChange,WorkflowPeriod_1st);

    }
    else if(TempWFPeriod==WorkflowPeriod_1st)
    {
        if(PidReqExcvSetVal(share_parameterCfgData_st.nominalCfg_st.MaxExciVoltage,30))
        {
            SetNewWorkFlowPeriodStatus(&TempWFPeriod,&WFStaChange,WorkflowPeriod_2nd);
        }
    }
    else
    {
        /* AlarmShutDown     */
        if(ShutDownAlarm_WorkStepHandle(&TempWFStep,&TempWFPeriod,&WFStaChange,GenFaultWay_ALL))
        {
            UpdateWorkflowModeStatus(&TempWorkflow, &TempWFStep,&TempWFPeriod, &WFStaChange);
            return (ActInform);
        }
        if(GetShareBufferDataContent(App_Sem_WORKFLOW, CfgOperaType_OSSem, &ShareBuffer_SharePIDOrderSta[ShareInOrderSta_ExcvSetVal])==PIDOrderSta_DEF_SUCCESS)
        {
            SetNextNewWorkFlowStepStatus(&TempWFStep,&TempWFPeriod,&WFStaChange,ExtExcitationStep_Maximum);
        }
    }
    /* Update Workflow--Step and Mode */
    UpdateWorkflowModeStatus(&TempWorkflow, &TempWFStep,&TempWFPeriod, &WFStaChange);
    return (ActInform);
}
/**
 * @brief 调节励磁电流到最小
 * 
 * @param TempWorkflow 
 * @param TempWFStep 
 * @param TempWFPeriod 
 * @param TempOutAlarm 
 * @return uint8_t 
 */
uint8_t ExtExcitationStep_4st(uint8_t TempWorkflow,uint8_t TempWFStep,uint8_t TempWFPeriod, uint8_t *TempOutAlarm)
{
    uint8_t ActInform = DEF_FALSE,WFStaChange = DEF_FALSE;
    if(TempWFPeriod==WorkflowPeriod_Entry)
    {
        if(PidReqExciLimVal(20))
        {
            SetNewWorkFlowPeriodStatus(&TempWFPeriod,&WFStaChange,WorkflowPeriod_1st);
        }
    }
    else
    {
        /* AlarmShutDown     */
        if(ShutDownAlarm_WorkStepHandle(&TempWFStep,&TempWFPeriod,&WFStaChange,GenFaultWay_ALL))
        {
            UpdateWorkflowModeStatus(&TempWorkflow, &TempWFStep,&TempWFPeriod, &WFStaChange);
            return (ActInform);
        }
        if(GetShareBufferDataContent(App_Sem_WORKFLOW, CfgOperaType_OSSem, &ShareBuffer_SharePIDOrderSta[ShareInOrderSta_ExciLimVal])==PIDOrderSta_DEF_SUCCESS)
        {
            SetNextNewWorkFlowStepStatus(&TempWFStep,&TempWFPeriod,&WFStaChange,ExtExcitationStep_Maximum);
        }
    }
    /* Update Workflow--Step and Mode */
    UpdateWorkflowModeStatus(&TempWorkflow, &TempWFStep,&TempWFPeriod, &WFStaChange);
    return (ActInform);
}
/**
 * @brief 调节励磁分流到预设值
 * 
 * @param TempWorkflow 
 * @param TempWFStep 
 * @param TempWFPeriod 
 * @param TempOutAlarm 
 * @return uint8_t 
 */
uint8_t ExtExcitationStep_5st(uint8_t TempWorkflow,uint8_t TempWFStep,uint8_t TempWFPeriod, uint8_t *TempOutAlarm)
{
    uint8_t ActInform = DEF_FALSE,WFStaChange = DEF_FALSE;
    if(TempWFPeriod==WorkflowPeriod_Entry)
    {
        // 闭合励磁分流
        ModifyFlagShareBufferDataContent(NULL,CfgOperaType_None,ShareBuffer_Outputs,RelayOutput_EXCI_SW,BitOperaOpt_Set);
        ActInform |= WorkflowOutRequestBit_RelayOut;
        SetNewWorkFlowPeriodStatus(&TempWFPeriod,&WFStaChange,WorkflowPeriod_1st);
    }
    else if(TempWFPeriod==WorkflowPeriod_1st)
    {
        if(PidReqExciSetVal(share_parameterCfgData_st.nominalCfg_st.MaxExciCurrent,30))
        {
            SetNewWorkFlowPeriodStatus(&TempWFPeriod,&WFStaChange,WorkflowPeriod_2nd);
        }
    }
    else
    {
        /* AlarmShutDown     */
        if(ShutDownAlarm_WorkStepHandle(&TempWFStep,&TempWFPeriod,&WFStaChange,GenFaultWay_ALL))
        {
            UpdateWorkflowModeStatus(&TempWorkflow, &TempWFStep,&TempWFPeriod, &WFStaChange);
            return (ActInform);
        }
        if(GetShareBufferDataContent(App_Sem_WORKFLOW, CfgOperaType_OSSem, &ShareBuffer_SharePIDOrderSta[ShareInOrderSta_ExciSetVal])==PIDOrderSta_DEF_SUCCESS)
        {
            SetNextNewWorkFlowStepStatus(&TempWFStep,&TempWFPeriod,&WFStaChange,ExtExcitationStep_Maximum);
        }
    }
    /* Update Workflow--Step and Mode */
    UpdateWorkflowModeStatus(&TempWorkflow, &TempWFStep,&TempWFPeriod, &WFStaChange);
    return (ActInform);
}
/**
 * @brief 波动率数据采集
 * 
 * @param TempWorkflow 
 * @param TempWFStep 
 * @param TempWFPeriod 
 * @param TempOutAlarm 
 * @return uint8_t 
 */
uint8_t ExtExcitationStep_6st(uint8_t TempWorkflow,uint8_t TempWFStep,uint8_t TempWFPeriod, uint8_t *TempOutAlarm)
{
    uint8_t ActInform = DEF_FALSE,WFStaChange = DEF_FALSE;
    /* AlarmShutDown     */
    if(ShutDownAlarm_WorkStepHandle(&TempWFStep,&TempWFPeriod,&WFStaChange,GenFaultWay_ALL))
    {
        UpdateWorkflowModeStatus(&TempWorkflow, &TempWFStep,&TempWFPeriod, &WFStaChange);
        return (ActInform);
    }

    switch (TempWFPeriod)
    {
    case WorkflowPeriod_Entry:
        // 等待稳定
        SetNewWorkFlowPeriodStatus(&TempWFPeriod,&WFStaChange,WorkflowPeriod_1st);
        SMTimerCounter = (uint32_t)share_parameterCfgData_st.ceilingExciCfg_st.WaitVoltStab_time * WorkFlowTimingFreq;
        break;
    case WorkflowPeriod_1st:
        if(SMTimerCounter==0)
        {
            SetNewWorkFlowPeriodStatus(&TempWFPeriod,&WFStaChange,WorkflowPeriod_2nd);
        }
        break;
    case WorkflowPeriod_2nd:
        // 波形
        ModifyFlagShareBufferDataContent(NULL, CfgOperaType_None, FlagShareBuffer_WFWave, WFWave_Waveform,BitOperaOpt_Set);
        // 期间数据
        ModifyFlagShareBufferDataContent(NULL, CfgOperaType_None, FlagShareBuffer_WFWave, WFWave_VolatilityCollect,BitOperaOpt_Set);
        ActInform |= WorkflowOutRequestBit_WaveRequest;

        SMTimerCounter = (uint32_t)share_parameterCfgData_st.ceilingExciCfg_st.MaxExciTest_timeout * WorkFlowTimingFreq;
        SetNewWorkFlowPeriodStatus(&TempWFPeriod,&WFStaChange,WorkflowPeriod_3nd);
        break;
    case WorkflowPeriod_3nd:
        if(SMTimerCounter==0)
        {
            SetNewWorkFlowPeriodStatus(&TempWFPeriod,&WFStaChange,WorkflowPeriod_4nd);
        }
        break;
    case WorkflowPeriod_4nd:
        ModifyFlagShareBufferDataContent(NULL, CfgOperaType_None, FlagShareBuffer_WFWave, WFWave_WaveformEnd,BitOperaOpt_Set);
        ModifyFlagShareBufferDataContent(NULL, CfgOperaType_None, FlagShareBuffer_WFWave, WFWave_VolatilityCollectEnd,BitOperaOpt_Set);
        ActInform |= WorkflowOutRequestBit_WaveRequest;
        SetNewWorkFlowPeriodStatus(&TempWFPeriod,&WFStaChange,WorkflowPeriod_5nd);
        break;
    case WorkflowPeriod_5nd:
        // 波动数据
        share_CeilingExciData_st.baseData_st.SteadyVoltage_U=TemSUM_GenPhaseVoltL1/TemCount;
        share_CeilingExciData_st.baseData_st.SteadyVoltage_V=TemSUM_GenPhaseVoltL2/TemCount;
        share_CeilingExciData_st.baseData_st.SteadyVoltage_W=TemSUM_GenPhaseVoltL3/TemCount;
        share_CeilingExciData_st.baseData_st.SteadyAverageVolt=TemSUM_GenPhaseVoltAverage/TemCount;
        // 计算
        share_CeilingExciData_st.baseData_st.MaxVoltVolat_U=TemMax_GenPhaseVoltL1;
        share_CeilingExciData_st.baseData_st.MaxVoltVolat_V=TemMax_GenPhaseVoltL2;
        share_CeilingExciData_st.baseData_st.MaxVoltVolat_W=TemMax_GenPhaseVoltL3;
        share_CeilingExciData_st.baseData_st.MaxVoltVolatility=TemMax_GenPhaseVoltAverage;
        share_CeilingExciData_st.baseData_st.MinVoltVolat_U=TemMin_GenPhaseVoltL1;
        share_CeilingExciData_st.baseData_st.MinVoltVolat_V=TemMin_GenPhaseVoltL2;
        share_CeilingExciData_st.baseData_st.MinVoltVolat_W=TemMin_GenPhaseVoltL3;
        share_CeilingExciData_st.baseData_st.MinVoltVolatility=TemMin_GenPhaseVoltAverage;

        // 测试期间的AVR平均励磁电压（发电机）
        share_CeilingExciData_st.baseData_st.GenExci_volt=TemSUM_ExcitationVolt/TemCount;
        // 测试期间的AVR平均励磁电流（发电机）
        share_CeilingExciData_st.baseData_st.GenExci_current=TemSUM_ExcitationCurr/TemCount;
        // 测试期间的AVR的平均励磁功率（发电机）
        share_CeilingExciData_st.baseData_st.GenExci_power=TemSUM_ExcitationPower/TemCount;
        SetNextNewWorkFlowStepStatus(&TempWFStep,&TempWFPeriod,&WFStaChange,ExtExcitationStep_endStep);
        break;
    }


    /* Update Workflow--Step and Mode */
    UpdateWorkflowModeStatus(&TempWorkflow, &TempWFStep,&TempWFPeriod, &WFStaChange);
    return (ActInform);
}
uint8_t ExtExcitationStep_end(uint8_t TempWorkflow,uint8_t TempWFStep,uint8_t TempWFPeriod, uint8_t *TempOutAlarm)
{
    uint8_t ActInform = DEF_FALSE,WFStaChange = DEF_FALSE;
    uint8_t WorkflowMode = TempWorkflow;
    if (TempWFPeriod == WorkflowPeriod_Entry)
    {
        SetNewWorkFlowPeriodStatus(&TempWFPeriod,&WFStaChange,WorkflowPeriod_1st);
        // 关闭励磁分流分压电源
        ModifyFlagShareBufferDataContent(NULL,CfgOperaType_None,ShareBuffer_Outputs,RelayOutput_EXCV_SW,BitOperaOpt_Clr);
        ModifyFlagShareBufferDataContent(NULL,CfgOperaType_None,ShareBuffer_Outputs,RelayOutput_EXCI_SW,BitOperaOpt_Clr);
        ActInform |= WorkflowOutRequestBit_RelayOut;
        CurrProcCountdownEnable=DEF_FALSE;
        ModifyFlagShareBufferDataContent(NULL, CfgOperaType_None, FlagShareBuffer_WFWave, WFWave_WaveformEnd,BitOperaOpt_Set);
        ActInform |= WorkflowOutRequestBit_WaveRequest;
    }
    else
    {
        FlowDataStatus |= (1<<WorkflowMode_ExtExcitation);
        if(GetFlagShareBufferDataContent(NULL,CfgOperaType_None,FlagShareBuffer_GenWorkingSta,GenWorkStaOrd_AlarmShutDown))
        {
            SetNewWorkflowModeStatus(&TempWorkflow,&TempWFStep,&TempWFPeriod,&WFStaChange,WorkflowMode_StopPriMover);
        }
        else
        {
            //正常停机计算数据
            SetNewWorkflowModeStatus(&TempWorkflow,&TempWFStep,&TempWFPeriod,&WFStaChange,WorkflowMode_Ending);
            WaveSetFlowDataStatus(WorkflowMode);
        }
    }
    /* Update Workflow--Step and Mode */
    UpdateWorkflowModeStatus(&TempWorkflow, &TempWFStep,&TempWFPeriod, &WFStaChange);
    return (ActInform);
}

typedef uint8_t (*WorkFlowStepType)(uint8_t TempWorkflow,uint8_t TempWFStep,uint8_t TempWFPeriod, uint8_t *TempOutAlarm);
static const WorkFlowStepType ExtExcitationStepTable[] = {
    (WorkFlowStepType)ExtExcitationStep_1st        ,
    (WorkFlowStepType)ExtExcitationStep_2st        ,
    (WorkFlowStepType)ExtExcitationStep_3st        ,
    (WorkFlowStepType)ExtExcitationStep_4st        ,
    (WorkFlowStepType)ExtExcitationStep_5st        ,
    (WorkFlowStepType)ExtExcitationStep_6st        ,
    (WorkFlowStepType)ExtExcitationStep_end        ,
};


uint8_t WorkFlowHandling_ExtExcitation(uint8_t TempWorkflow, uint8_t TempWFStep,uint8_t TempWFPeriod, uint8_t *TempOutAlarm)
{
    uint8_t ActInform = DEF_FALSE, WFStaChange = DEF_FALSE;

	ActInform |= (*ExtExcitationStepTable[TempWFStep])(TempWorkflow,TempWFStep,TempWFPeriod, TempOutAlarm);

    /************************************/
    /* Update Workflow--Step and Mode */
    UpdateWorkflowModeStatus(&TempWorkflow, &TempWFStep,&TempWFPeriod, &WFStaChange);
    return (ActInform);
}
#endif